package com.yjlove.myplay.mvptest.presenter;


import android.os.Handler;

import com.yjlove.myplay.mvptest.bean.User;
import com.yjlove.myplay.mvptest.biz.IUserBiz;
import com.yjlove.myplay.mvptest.biz.OnLoginListener;
import com.yjlove.myplay.mvptest.biz.UserBiz;
import com.yjlove.myplay.mvptest.ui.IUserLoginView;
import com.yjlove.myplay.utils.SP;
import com.yjlove.myplay.common.StringSpKey;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者 YJlvoe
 * 创建时间 2017/1/18.
 */
public class UserLoginPresenter {
    private IUserBiz userBiz;
    private IUserLoginView userLoginView;
    private Handler mHandler = new Handler();

    public UserLoginPresenter(IUserLoginView userLoginView)
    {
        this.userLoginView = userLoginView;
        this.userBiz = new UserBiz();
    }

    public void login()
    {
        userLoginView.showLoading();
        userBiz.login(userLoginView.getUserName(), userLoginView.getPassword(), new OnLoginListener()
        {
            @Override
            public void loginSuccess(final User user)
            {
                //需要在UI线程执行
                mHandler.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        userLoginView.toMainActivity(user);
                        userLoginView.hideLoading();
                    }
                });

            }

            @Override
            public void loginFailed()
            {
                //需要在UI线程执行
                mHandler.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        userLoginView.showFailedError();
                        userLoginView.hideLoading();
                    }
                });
            }
        });
    }

    public void clear()
    {
        userLoginView.clearUserName();
        userLoginView.clearPassword();
    }

    public List<String> showText(){
        List<String> list = new ArrayList<String>();
        list.add(SP.get(StringSpKey.USER_NAME, ""));
        list.add(SP.get(StringSpKey.PASS_WORD, ""));
        return list;
    }
}
